/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.general.identification;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.general.GeneralConstants;
import gov.fema.hazus.bdc.shared.entity.Address;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.Contact;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;


public class IdentificationForm extends AbstractForm implements BuildingUpdate {
	
	// labels
	private Label name;
	private Label number;
	private Label abbreviation;
	private Label address;
	private Label city;
	private Label state;
	private Label postalCode;
	private Label owner;
	private Label contact;
	private Label phone;
	
	// form fields
	private TextBox formName = new TextBox();
	private TextBox formNumber = new TextBox();
	private TextBox formAbbreviation = new TextBox();
	private TextBox formAddress = new TextBox();
	private TextBox formCity = new TextBox();
	private USStateListBox formState = new USStateListBox();
	private TextBox formPostalCode = new TextBox();
	private TextBox formOwner = new TextBox();
	private TextBox formContactPerson = new TextBox();
	private TextBox formPhone = new TextBox();
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(10,2);
	
	// entities
	private Address addr;
	private Contact cont;
	
	// listeners
	private BuildingTextBoxKeyListener buildingTextBoxKeyListener;
	private AddressListBoxChangeListener addressListBoxChangeListener;
	private AddressTextBoxKeyListener addressTextBoxKeyListener;
	private ContactTextBoxKeyListener contactTextBoxKeyListener;
	private GeneralConstants constants;

	// gmap
	//private GMap2 gmap;
	
	public IdentificationForm(Building building) {
		
		// do parent
		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (GeneralConstants)GWT.create(GeneralConstants.class);
		tabTitle = constants.generalIdentificationTabTitle();
		
		// set up labels
		name = new Label(constants.generalIdentificationName());
		number = new Label(constants.generalIdentificationNumber());
		abbreviation = new Label(constants.generalIdentificationAbbreviation());
		address = new Label(constants.generalIdentificationAddress());
		city = new Label(constants.generalIdentificationCity());
		state = new Label(constants.generalIdentificationState());
		postalCode = new Label(constants.generalIdentificationPostalCode());
		owner = new Label(constants.generalIdentificationOwner());
		contact = new Label(constants.generalIdentificationContact());
		phone = new Label(constants.generalIdentificationPhone());

		// handle entities
		addr = building.getAddress();
		cont = building.getContact();		
		
		// set up listeners
		buildingTextBoxKeyListener = new BuildingTextBoxKeyListener(building);
		
		addressListBoxChangeListener = new AddressListBoxChangeListener(addr);
		addressTextBoxKeyListener = new AddressTextBoxKeyListener(addr);
		
		contactTextBoxKeyListener = new ContactTextBoxKeyListener(cont);

		// load listeners (only on instantiation)
		loadListeners();
		
		// load forms
		loadForms();
		
		// add labels to form
		table.setWidget(0, 0, name);
		table.setWidget(1, 0, number);
		table.setWidget(2, 0, abbreviation);
		table.setWidget(3, 0, address);
		table.setWidget(4, 0, city);
		table.setWidget(5, 0, state);
		table.setWidget(6, 0, postalCode);
		table.setWidget(7, 0, owner);
		table.setWidget(8, 0, contact);
		table.setWidget(9, 0, phone);
		

		// add fields to table
		table.setWidget(0, 1, formName);
		table.setWidget(1, 1, formNumber);
		table.setWidget(2, 1, formAbbreviation);
		table.setWidget(3, 1, formAddress);
		table.setWidget(4, 1, formCity);
		table.setWidget(5, 1, formState);
		table.setWidget(6, 1, formPostalCode);
		table.setWidget(7, 1, formOwner);
		table.setWidget(8, 1, formContactPerson);
		table.setWidget(9, 1, formPhone);
		
	    docker.add(table, DockPanel.CENTER);
		
	    
		initWidget(docker);

	}
	
	public void updateBuilding(Building building)
	{
		
		// entities
		this.building = building;
		this.addr = building.getAddress();
		this.cont = building.getContact();
		
		// listeners
		buildingTextBoxKeyListener.updateBuilding(building);
		addressListBoxChangeListener.updateAddress(addr);
		addressTextBoxKeyListener.updateAddress(addr);
		contactTextBoxKeyListener.updateContact(cont);
		
		// update map
		/*
	    double lat = building.getLocation().getLatitude();
	    double lng = building.getLocation().getLongitude();
	    GWT.log("building location="+building.getLocation(), null);
	    GWT.log("lat="+lat, null);
	    GWT.log("lng="+lng, null);
	    GLatLng center = new GLatLng(lat,lng);
	    gmap.panTo(center);		
		*/
		
		// reload forms, but not listener
		loadForms();
		
	}
	
	private void loadListeners()
	{
		formName.addFocusListener( buildingTextBoxKeyListener );
		formNumber.addFocusListener( buildingTextBoxKeyListener );
		formAbbreviation.addFocusListener( buildingTextBoxKeyListener );
		formAddress.addFocusListener(addressTextBoxKeyListener);
		formCity.addFocusListener(addressTextBoxKeyListener);
		formState.addChangeListener(addressListBoxChangeListener);
		formPostalCode.addFocusListener(addressTextBoxKeyListener);
		formContactPerson.addFocusListener( contactTextBoxKeyListener );
		formPhone.addFocusListener( contactTextBoxKeyListener );
		
	}
	
	private void loadForms()
	{
		
		if( building != null)
		{
			formName.setText( building.getName() );
			formName.setName( Building.NAME );
			
			formNumber.setText( building.getNumber() );
			formNumber.setName( Building.NUMBER );
			
			formAbbreviation.setText( building.getAbbreviation() );
			formAbbreviation.setName( Building.ABBREVIATION );
			
			if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formAbbreviation.setEnabled(false);
	            formAddress.setEnabled(false);
	            formCity.setEnabled(false);
	            formContactPerson.setEnabled(false);
	            formName.setEnabled(false);
	            formNumber.setEnabled(false);
	            formOwner.setEnabled(false);
	            formPhone.setEnabled(false);
	            formPostalCode.setEnabled(false);
	            formState.setEnabled(false);
	        } else {
	            formAbbreviation.setEnabled(true);
	            formAddress.setEnabled(true);
	            formCity.setEnabled(true);
	            formContactPerson.setEnabled(true);
	            formName.setEnabled(true);
	            formNumber.setEnabled(true);
	            formOwner.setEnabled(true);
	            formPhone.setEnabled(true);
	            formPostalCode.setEnabled(true);
	            formState.setEnabled(true);
	        }
		
		}
		if( addr != null)
		{	
			formAddress.setText(addr.getStreet1());
			formAddress.setName(Address.STREET1);
			
			formCity.setText(addr.getCity());
			formCity.setName(Address.CITY);
			
			formState.setListItem(addr.getState());
			formState.setName(Address.STATE);
			
			formPostalCode.setText(addr.getPostalCode());
			formPostalCode.setName(Address.POSTALCODE);
			
		}
		if( cont != null) 
		{
			formOwner.setText(cont.getOwner());
			formOwner.setName( Contact.OWNER );
			formOwner.addFocusListener( contactTextBoxKeyListener );
			
			formContactPerson.setText(cont.getContactPerson());
			formContactPerson.setName( Contact.CONTACTPERSON );
			
			formPhone.setText(cont.getPhone());
			formPhone.setName( Contact.PHONE );
		}
	        
		
        
	}

	public void updateName() {
		building.setName( formName.getText() );
	}
}
